<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>

<link rel="stylesheet" media="screen" href="css/docs.css"/>
<link rel="stylesheet" media="screen" href="lib/jqplot/jquery.jqplot.min.css"/>
<link rel="stylesheet" media="screen" href="lib/datatables/css/vpad.css"/>

<script type="text/javascript" src="${ctx}/js/global_util.js"></script>

<h1 class="page-title">出差申请单 - 添加</h1>

<div class="container_12 clearfix leading">
    <h5>流水号：<strong id="d_serialNumber"> </strong> 申请时间：<strong id="d_date"></strong></h5>

    <div class="side grid_12 leading">
        <form class="form has-validation" id="post_form" name="post_form">
            <input id="a_serialNumber" name="applyForm.serialNumber" type="hidden">
            <div class="clear"></div>
            <div class="clearfix">
                <label for="a_evectionReason" class="form-label">出差事由 <em>*</em></label>
                <div class="form-input">
                    <select id="a_evectionReason" name="applyForm.evectionReason">
                        <option>公务</option>
                        <option>培训</option>
                        <option>休假</option>
                        <option>其他</option>
                    </select>
                </div>
            </div>

            <div class="clear"></div>
            <div class="clearfix">
                <label for="a_otherDescribe" class="form-label">其他描述 <em>*</em></label>
                <div class="form-input">
                    <input type="text" id="a_otherDescribe" name="applyForm.otherDescribe" required="required" placeholder="" value="${applyForm.otherDescribe}"/>
                </div>
            </div>

            <div class="clear"></div>
            <div class="clearfix">
                <label for="a_evectionAddress" class="form-label">出差地点 <em>*</em></label>
                <div class="form-input">
                    <input type="text" id="a_evectionAddress" name="applyForm.evectionAddress" required="required" placeholder="" value="${applyForm.evectionAddress}"/>
                </div>
            </div>

            <div class="clear"></div>
            <div class="clearfix">
                <label for="a_evectionStartDate" class="form-label">出差时间自 <em>*</em></label>
                <div class="form-input"><input type="text" class="date" id="a_evectionStartDate" name="s_evectionStartDate" onfocus="WdatePicker({maxDate:$('#a_evectionEndDate').val()})" readonly="readonly" value='<s:date name="#request.applyForm.evectionStartDate" format="yyyy-MM-dd"/>'/>
                </div>
            </div>

            <div class="clear"></div>
            <div class="clearfix">
                <label for="a_evectionEndDate" class="form-label">出差时间至 <em>*</em></label>
                <div class="form-input"><input type="text" class="date" id="a_evectionEndDate" name="s_evectionEndDate" onfocus="WdatePicker({minDate:$('#a_evectionStartDate').val()})" readonly="readonly" value='<s:date name="#request.applyForm.evectionEndDate" format="yyyy-MM-dd"/>'/> </div>
            </div>

            <div class="clear"></div>
            <div class="clearfix">
                <label for="a_days" class="form-label">共计时间为 <em>*</em></label>
                <div class="form-input">
                    <input type="text" id="a_days" name="applyForm.days" required="required" placeholder="请输入天数" value="${applyForm.days}"/>
                    <input type="text" id="a_hours" name="applyForm.hours" required="required" placeholder="请输入小时数" value="${applyForm.hours}"/>
                </div>
            </div>

            <div class="clear"></div>
            <div class="clearfix">
                <label class="form-label" for="help_department_names">协助部门 <em>*</em></label>
                <div class="form-input">
                    <span style="font-weight: 800;font-size: 16px;margin-right: 10px;margin-left:5px;display: none;" id="help_department_names">
                        <s:iterator value="#request.helpDepartmentList" var="department">
                            <span>${department.organization.fullName}</span>
                            <input type="hidden" name="help_department" value="${department.organization.id}-${department.organization.fullName}-${department.content}"/>
                        </s:iterator>
                    </span>
                    <a style=" margin-top:10px; margin-left:10px" href="javascript:void(0)" onclick="choiceHelpDepartment()" class="button"><span class="icon icon-ok" style="background:url(${ctx}/images/img/cursor.png)">&nbsp;</span>选择</a>
                </div>
            </div>

            <div class="clear"></div>
            <div class="clearfix">
                <label for="a_planeCost" class="form-label">机/车票 <em>*</em></label>
                <div class="form-input">
                    <input type="text" id="a_planeCost" name="applyForm.planeCost" required="required" placeholder="" value="${applyForm.planeCost}"/>
                </div>
            </div>

            <div class="clear"></div>
            <div class="clearfix">
                <label for="a_trafficCost" class="form-label">交通费 <em>*</em></label>
                <div class="form-input">
                    <input type="text" id="a_trafficCost" name="applyForm.trafficCost" required="required" placeholder="" value="${applyForm.trafficCost}"/>
                </div>
            </div>

            <div class="clear"></div>
            <div class="clearfix">
                <label for="a_stayCost" class="form-label">住宿费 <em>*</em></label>
                <div class="form-input">
                    <input type="text" id="a_stayCost" name="applyForm.stayCost" required="required" placeholder="" value="${applyForm.stayCost}"/>
                </div>
            </div>

            <div class="clear"></div>
            <div class="clearfix">
                <label for="a_mealCost" class="form-label">餐费 <em>*</em></label>
                <div class="form-input">
                    <input type="text" id="a_mealCost" name="applyForm.mealCost" required="required" placeholder="" value="${applyForm.mealCost}"/>
                </div>
            </div>

            <div class="clear"></div>
            <div class="clearfix">
                <label for="a_otherCost" class="form-label">其他 <em>*</em></label>
                <div class="form-input">
                    <input type="text" id="a_otherCost" name="applyForm.otherCost" required="required" placeholder="" value="${applyForm.otherCost}"/>
                </div>
            </div>

            <div class="clear"></div>
            <div class="clearfix">
                <label for="a_amount" class="form-label">费用合计为 <em>*</em></label>
                <div class="form-input">
                    <input type="text" id="a_amount" name="applyForm.amount" required="required" placeholder="" value="${applyForm.amount}"/>
                </div>
            </div>

            <div class="form-action clearfix">
                <button class="button" type="button" onclick="checkForm()">添加</button>
                <button class="button" type="reset">重置</button>
                <a class="button" href="#${userType==1?"sign" : "public"}/administration-evection-apply-form.xhtml"><span style="background:url(${ctx}/images/img/arrow_undo.png)" class="icon">&nbsp;</span>返回</a>
            </div>
        </form>
    </div>
</div>

<form id="session_form" name="session_form"></form>

<div class="clear"></div>

<script type="text/javascript" src="js/Tree.js"></script>
<script type="text/javascript">
    $(function () {
        if($("#help_department_names").html().length > 0){
            $("#help_department_names").show();
        }

        var serial = createSerialNumber();
        $("#d_serialNumber").html(serial);
        $("#a_serialNumber").val(serial);
        var date = new Date();
        $("#d_date").html(date.getFullYear()+"年"+(date.getMonth()+1)+"月"+date.getDate()+"日");
    });

    function checkForm() {
        var a_otherDescribe = $("#a_otherDescribe").val();
        if (checkNull(a_otherDescribe)) {
            alert("请填写收其他描述");
            return;
        }
        var a_evectionAddress = $("#a_evectionAddress").val();
        if (checkNull(a_evectionAddress)) {
            alert("请填写出差地点");
            return;
        }
        var a_evectionStartDate = $("#a_evectionStartDate").val();
        if (checkNull(a_evectionStartDate)) {
            alert("请填写开始出差时间");
            return;
        }
        var a_evectionEndDate = $("#a_evectionEndDate").val();
        if (checkNull(a_evectionEndDate)) {
            alert("请填写结束出差时间");
            return;
        }
        var a_days = $("#a_days").val();
        if (checkNull(a_days) || isNaN(a_days)) {
            alert("请正确填写共计时间数");
            return;
        }
        if($("input[type='hidden'][name='help_department']").length == 0){
            alert("请选择协助部门")
            return;
        }
        var a_planeCost = $("#a_planeCost").val();
        if (checkNull(a_planeCost) || isNaN(a_planeCost)) {
            alert("请正确填写机/车票");
            return;
        }
        var a_trafficCost = $("#a_trafficCost").val();
        if (checkNull(a_trafficCost) || isNaN(a_trafficCost)) {
            alert("请正确填写交通费");
            return;
        }
        var a_stayCost = $("#a_stayCost").val();
        if (checkNull(a_stayCost) || isNaN(a_stayCost)) {
            alert("请正确填写住宿费");
            return;
        }
        var a_mealCost = $("#a_mealCost").val();
        if (checkNull(a_mealCost) || isNaN(a_mealCost)) {
            alert("请正确填写餐费");
            return;
        }
        var a_otherCost = $("#a_otherCost").val();
        if (checkNull(a_otherCost) || isNaN(a_otherCost)) {
            alert("请填正确写其他费");
            return;
        }
        var a_amount = $("#a_amount").val();
        if (checkNull(a_amount) || isNaN(a_amount)) {
            alert("请正确填写费用合计");
            return;
        }

        var data = $("#post_form").serialize();
        var url = "${ctx}/${userType==1?"sign" : "public"}/administration-evection-apply-form-save.xhtml";
        $.ajax(url, {
            type:"POST",
            data:data,
            dataType:"html",
            cache:false,
            success:function (a) {
                return pageDownloaded(a, url)
            },
            complete:function () {
            }
        });
    }

    //选择协助部门时，先将填写的表单保存到session
    function choiceHelpDepartment(){
        var a_otherDescribe = $("#a_otherDescribe").val();
        if (!checkNull(a_otherDescribe)) {
            $("#session_form").append("<input type='' name='applyForm.otherDescribe' value='"+a_otherDescribe+"'>");
        }
        var a_evectionAddress = $("#a_evectionAddress").val();
        if (!checkNull(a_evectionAddress)) {
            $("#session_form").append("<input type='' name='applyForm.evectionAddress' value='"+a_evectionAddress+"'>")
        }
        var a_evectionStartDate = $("#a_evectionStartDate").val();
        if (!checkNull(a_evectionStartDate)) {
            $("#session_form").append("<input type='' name='s_evectionStartDate' value='"+a_evectionStartDate+"'>")
        }
        var a_evectionEndDate = $("#a_evectionEndDate").val();
        if (!checkNull(a_evectionEndDate)) {
            $("#session_form").append("<input type='' name='s_evectionEndDate' value='"+a_evectionEndDate+"'>")
        }
        var a_days = $("#a_days").val();
        if (!checkNull(a_days)) {
            $("#session_form").append("<input type='' name='applyForm.days' value='"+a_days+"'>")
        }
        var a_hours = $("#a_hours").val();
        if (!checkNull(a_hours)) {
            $("#session_form").append("<input type='' name='applyForm.hours' value='"+a_hours+"'>")
        }
        var a_planeCost = $("#a_planeCost").val();
        if (!checkNull(a_planeCost)) {
            $("#session_form").append("<input type='' name='applyForm.planeCost' value='"+a_planeCost+"'>")
        }
        var a_trafficCost = $("#a_trafficCost").val();
        if (!checkNull(a_trafficCost)) {
            $("#session_form").append("<input type='' name='applyForm.trafficCost' value='"+a_trafficCost+"'>")
        }
        var a_stayCost = $("#a_stayCost").val();
        if (!checkNull(a_stayCost)) {
            $("#session_form").append("<input type='' name='applyForm.stayCost' value='"+a_stayCost+"'>")
        }
        var a_mealCost = $("#a_mealCost").val();
        if (!checkNull(a_mealCost)) {
            $("#session_form").append("<input type='' name='applyForm.mealCost' value='"+a_mealCost+"'>")
        }
        var a_otherCost = $("#a_otherCost").val();
        if (!checkNull(a_otherCost)) {
            $("#session_form").append("<input type='' name='applyForm.otherCost' value='"+a_otherCost+"'>")
        }
        var a_amount = $("#a_amount").val();
        if (!checkNull(a_amount)) {
            $("#session_form").append("<input type='' name='applyForm.amount' value='"+a_amount+"'>")
        }

        var data = $("#session_form").serialize();
        if(data != null && data.length > 0){
            var url = "${ctx}/ajax/administration-evection-apply-form-save.xhtml";
            $.ajax(url, {
                type:"POST",
                data:data,
                dataType:"html",
                cache:false,
                success:function (a) {
                    window.location.href = "#public/help-department-main.xhtml?type=${userType}";
                },
                complete:function () {
                    window.location.href = "#public/help-department-main.xhtml?type=${userType}";
                }
            });
        }else{
            window.location.href = "#public/help-department-main.xhtml?type=${userType}";
        }
    }

</script>